#include "GUIManager.h"

template<> GUIManager* Ogre::Singleton<GUIManager>::ms_Singleton = 0;

GUIManager::GUIManager(Ogre::RenderWindow* mWindow) :
	mGUISystem(0),
	mGUIRenderer(0),
	mDefaultSheet(0),
	mRenderWindow(mWindow),
	displayed(true)
{
	// Setup GUI system -- initialize
	Ogre::SceneManager* mSceneMgr = Root::getSingletonPtr()->createSceneManager(ST_GENERIC, "DEF");
	mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
	mGUISystem = new CEGUI::System(mGUIRenderer);
	CEGUI::SchemeManager::getSingleton().loadScheme( (CEGUI::utf8*)"TaharezLookSkin.scheme");

	// Load scheme and set up defaults -- by constructor
	mGUISystem->setDefaultMouseCursor( (CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
	mGUISystem->setDefaultFont((CEGUI::utf8*)"BlueHighway-12");
	CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
	CEGUI::MouseCursor::getSingleton().show( );

	createIntroGUI();
	createNetGUI();
	createQuitMenu();
}

GUIManager::~GUIManager()
{
	if(mGUISystem)
		delete mGUISystem;
	if(mGUIRenderer)
		delete mGUIRenderer;
	mGUISystem = 0;
	mGUIRenderer = 0;
}

void GUIManager::registerScene(Ogre::SceneManager* mSceneMgr)
{
	((CEGUI::OgreCEGUIRenderer*)mGUIRenderer)->setTargetSceneManager(mSceneMgr);
}

void GUIManager::setGUISheet(CEGUI::Window* sheet)
{
	mGUISystem->setGUISheet(sheet);
}

bool GUIManager::isDisplayed()
{
	return displayed;
}

void GUIManager::show()
{
	mGUISystem->getGUISheet()->show();
	CEGUI::MouseCursor::getSingletonPtr()->show();
	displayed = true;
}

void GUIManager::hide()
{
	mGUISystem->getGUISheet()->hide();
	CEGUI::MouseCursor::getSingletonPtr()->hide();
	displayed = false;
}

void GUIManager::createIntroGUI()
{
	// Setting up the background window sheet
	CEGUI::WindowManager *win = CEGUI::WindowManager::getSingletonPtr();
    CEGUI::Window *sheet = win->createWindow("DefaultGUISheet", "Intro/Sheet");
	
	// Create the Play button inside the default sheet
	CEGUI::Window *play = win->createWindow("TaharezLook/Button", "Intro/PlayButton");
    play->setText("Play");
    play->setSize(CEGUI::UVector2(CEGUI::UDim(0.10f, 0), CEGUI::UDim(0.04f, 0)));
	play->setPosition( CEGUI::UVector2(CEGUI::UDim(0.45f,0),CEGUI::UDim(0.7f,0)) );
	sheet->addChildWindow(play);
	
	// Create the Quit button inside the default sheet
	CEGUI::Window *quit = win->createWindow("TaharezLook/Button", "Intro/QuitButton");
    quit->setText("Quit");
    quit->setSize(CEGUI::UVector2(CEGUI::UDim(0.10f, 0), CEGUI::UDim(0.04f, 0)));
	quit->setPosition( CEGUI::UVector2(CEGUI::UDim(0.45f,0),CEGUI::UDim(0.8f,0)) );
	sheet->addChildWindow(quit);
}

void GUIManager::createNetGUI()
{
	// Setting up the background window sheet
	CEGUI::WindowManager *win = CEGUI::WindowManager::getSingletonPtr();
    CEGUI::Window *sheet = win->createWindow("DefaultGUISheet", "Net/Sheet");
	
	// Create the Start button inside the default sheet
	CEGUI::Window *start = win->createWindow("TaharezLook/Button", "Net/StartButton");
    start->setText("Start");
    start->setSize(CEGUI::UVector2(CEGUI::UDim(0.10f, 0), CEGUI::UDim(0.04f, 0)));
	start->setPosition( CEGUI::UVector2(CEGUI::UDim(0.45f,0),CEGUI::UDim(0.7f,0)) );
	sheet->addChildWindow(start);
	
	// Create the Back button inside the default sheet
	CEGUI::Window *back = win->createWindow("TaharezLook/Button", "Net/BackButton");
    back->setText("Back");
    back->setSize(CEGUI::UVector2(CEGUI::UDim(0.10f, 0), CEGUI::UDim(0.04f, 0)));
	back->setPosition( CEGUI::UVector2(CEGUI::UDim(0.45f,0),CEGUI::UDim(0.8f,0)) );
	sheet->addChildWindow(back);
}

void GUIManager::createQuitMenu()
{
	// Setting up the background window sheet
	CEGUI::WindowManager *win = CEGUI::WindowManager::getSingletonPtr();
    CEGUI::Window *sheet = win->createWindow("DefaultGUISheet", "Quit/Sheet");
	
	// Create the Start button inside the default sheet
	CEGUI::Window *quit = win->createWindow("TaharezLook/Button", "Quit/QuitButton");
    quit->setText("Quit");
    quit->setSize(CEGUI::UVector2(CEGUI::UDim(0.10f, 0), CEGUI::UDim(0.04f, 0)));
	quit->setPosition( CEGUI::UVector2(CEGUI::UDim(0.45f,0),CEGUI::UDim(0.7f,0)) );
	sheet->addChildWindow(quit);
	
	// Create the Back button inside the default sheet
	CEGUI::Window *cancel = win->createWindow("TaharezLook/Button", "Quit/CancelButton");
    cancel->setText("Cancel");
    cancel->setSize(CEGUI::UVector2(CEGUI::UDim(0.10f, 0), CEGUI::UDim(0.04f, 0)));
	cancel->setPosition( CEGUI::UVector2(CEGUI::UDim(0.45f,0),CEGUI::UDim(0.8f,0)) );
	sheet->addChildWindow(cancel);
}